package com.quanyan.place.service.impl;

import static com.quanyan.common.utils.DateUtils.strToDate;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import baidu.webapi.LBSApi;
import baidu.webapi.impl.BaiduLBS;
import baidu.webapi.model.AddressComponent;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.http.HttpClientUtils;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.common.utils.DistanceUtil;
import com.quanyan.common.utils.TimeUtils;
import com.quanyan.place.cache.PlaceRedisTemplate;
import com.quanyan.place.client.PlaceClient;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.Cagetory;
import com.quanyan.place.entity.CagetoryExample;
import com.quanyan.place.entity.Dict;
import com.quanyan.place.entity.DictExample;
import com.quanyan.place.entity.PlaceDubboProvider;
import com.quanyan.place.entity.PlaceGroupInfo;
import com.quanyan.place.entity.PlaceUnitDubboProvider;
import com.quanyan.place.entity.TbBizPlace;
import com.quanyan.place.entity.TbBizPlaceAttach;
import com.quanyan.place.entity.TbBizPlaceAttachExample;
import com.quanyan.place.entity.TbBizPlaceCategory;
import com.quanyan.place.entity.TbBizPlaceCategoryExample;
import com.quanyan.place.entity.TbBizPlaceExample;
import com.quanyan.place.entity.TbBizPlaceOrder;
import com.quanyan.place.entity.TbBizPlaceOrderExample;
import com.quanyan.place.entity.TbBizPlaceUnit;
import com.quanyan.place.entity.TbBizPlaceUnitExample;
import com.quanyan.place.entity.TbBizPlaceUnitSalePlan;
import com.quanyan.place.entity.TbBizPlaceUnitSalePlanExample;
import com.quanyan.place.entity.TbBizPlaceUnitStatistics;
import com.quanyan.place.entity.TbBizPlaceUnitTime;
import com.quanyan.place.entity.TbBizPlaceUnitTimeExample;
import com.quanyan.place.entity.TbCity;
import com.quanyan.place.entity.TbCityExample;
import com.quanyan.place.entity.TbDistrict;
import com.quanyan.place.entity.TbDistrictExample;
import com.quanyan.place.entity.TbProvince;
import com.quanyan.place.entity.TbProvinceExample;
import com.quanyan.place.entity.TbSupplier;
import com.quanyan.place.entity.apiresp.RespCity;
import com.quanyan.place.entity.apiresp.RespCityInfos;
import com.quanyan.place.entity.apiresp.RespDistrict;
import com.quanyan.place.entity.apiresp.RespPlaceLocation;
import com.quanyan.place.entity.apiresp.RespRecommendPlaceOrder;
import com.quanyan.place.entity.vo.PlaceCommonDataVo;
import com.quanyan.place.entity.vo.RespDistrictVo;
import com.quanyan.place.entity.vo.TbBizPlaceUnitVo;
import com.quanyan.place.entity.vo.VPlaceTime;
import com.quanyan.place.mapper.CagetoryMapper;
import com.quanyan.place.mapper.DictMapper;
import com.quanyan.place.mapper.PlaceAttachMapper;
import com.quanyan.place.mapper.PlaceMapper;
import com.quanyan.place.mapper.PlaceUnitMapper;
import com.quanyan.place.mapper.TbAdminUserMapper;
import com.quanyan.place.mapper.TbBizPlaceAttachMapper;
import com.quanyan.place.mapper.TbBizPlaceCategoryMapper;
import com.quanyan.place.mapper.TbBizPlaceMapper;
import com.quanyan.place.mapper.TbBizPlaceOrderManagerMapper;
import com.quanyan.place.mapper.TbBizPlaceOrderMapper;
import com.quanyan.place.mapper.TbBizPlacePeriodMapper;
import com.quanyan.place.mapper.TbBizPlaceRepresentMapper;
import com.quanyan.place.mapper.TbBizPlaceUnitMapper;
import com.quanyan.place.mapper.TbBizPlaceUnitSalePlanMapper;
import com.quanyan.place.mapper.TbBizPlaceUnitStatisticsMapper;
import com.quanyan.place.mapper.TbBizPlaceUnitTimeMapper;
import com.quanyan.place.mapper.TbCityMapper;
import com.quanyan.place.mapper.TbDistrictMapper;
import com.quanyan.place.mapper.TbPlaceImageMapper;
import com.quanyan.place.mapper.TbProvinceMapper;
import com.quanyan.place.mapper.TbSupplierMapper;
import com.quanyan.place.mapper.VPlaceMapper;
import com.quanyan.place.mapper.VPlaceMemberMapper;
import com.quanyan.place.response.PlaceBaseResponse;
import com.quanyan.place.response.PlaceSearchResponse;
import com.quanyan.platform.response.MyPlaceResponse;
import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.search.entity.request.ReqPlaceSearchInfo;
import com.quanyan.search.entity.response.SearchResponses;
import com.quanyan.search.service.PlaceSearchService;
import com.quanyan.search.service.impl.PlaceSearchServiceImpl;


/**
 * Created by Administrator on 2016/4/14 0014.
 */
@Service("placeClient")
public class PlaceServiceImpl implements PlaceClient{
    private static final Logger logger = LoggerFactory.getLogger(PlaceServiceImpl.class);
    @Autowired
    DictMapper dictMapper;

    @Autowired
    CagetoryMapper cagetoryMapper;
    @Autowired
    PlaceMapper placeMapperBase;

    @Autowired
    TbBizPlaceMapper placeMapper;

    @Autowired
    TbAdminUserMapper adminUserMapper;

    @Autowired
    TbBizPlaceUnitMapper placeUnitMapper;

    @Autowired
    VPlaceMapper vPlaceMapper;

    @Autowired
    VPlaceMemberMapper vPlaceMemberMapper;

    @Autowired
    TbBizPlaceAttachMapper tbBizPlaceAttachMapper;

    @Autowired
    TbSupplierMapper tbSupplierMapper;

    @Autowired
    TbProvinceMapper tbProvinceMapper;

    @Autowired
    TbCityMapper tbCityMapper;

    @Autowired
    TbDistrictMapper tbDistrictMapper;

    @Autowired
    TbPlaceImageMapper tbPlaceImageMapper;

    @Autowired
    PlaceUnitMapper placeUnitListMapper;

    @Autowired
    PlaceAttachMapper placeAttachMapper;

    @Autowired
    PlaceMapper pMapper;

    @Autowired
    TbBizPlaceUnitStatisticsMapper tbBizPlaceUnitStatisticsMapper;

    @Autowired
    TbBizPlaceUnitTimeMapper tbBizPlaceUnitTimeMapper;

    @Autowired
    TbBizPlaceCategoryMapper tbBizPlaceCategoryMapper;

    @Autowired
    TbBizPlacePeriodMapper tbBizPlacePeriodMapper;

    @Autowired
    TbBizPlaceRepresentMapper tbBizPlaceRepresentMapper;

    //static Map<String,String> placeCommonDataMap = new HashMap<String,String>();

    @Autowired
    PlaceRedisTemplate placeRedisTemplate;

    @Autowired
    TbBizPlaceOrderManagerMapper tbBizPlaceOrderManagerMapper;
    @Autowired
    TbBizPlaceUnitSalePlanMapper tbBizPlaceUnitSalePlanMapper;

    @Value("${c.service.url}")
    public String cAddress;

    @Autowired
    TbBizPlaceOrderMapper tbBizPlaceOrderMapper;

    @Value("${SOLRCLOUD.SERVER.URL}")
    private String solrUrl;

    public APIResponse getPlaceCommonData() {
        PlaceCommonDataVo placeCommonDataVo = new PlaceCommonDataVo();
        Class<PlaceCommonDataVo> cls = PlaceCommonDataVo.class;
        Map<String,String> condition = new HashMap<String,String>();

        condition.put("网球场地类型","tennis_unit_type");
        condition.put("羽毛球场地类型","badminton_unit_type");
        condition.put("篮球场地类型","basketball_unit_type");
        condition.put("足球场地类型","football_unit_type");

        condition.put("网球场地材质","tennis_unit_texture");
        condition.put("羽毛球场地材质","badminton_unit_texture");
        condition.put("篮球场地材质","basketball_unit_texture");
        condition.put("足球场地材质","football_unit_texture");

        condition.put("灯光类型","light_type");
        condition.put("时间类型","time_type");
        condition.put("场馆属性","manage_type");
        condition.put("足球场地规模","football_field_scale");

        try {
            Set<Map.Entry<String,String>> set = condition.entrySet();
            for (Map.Entry<String,String> map : set){
                DictExample dictExample = new DictExample();
                DictExample.Criteria dictCriteria = dictExample.createCriteria();
                dictCriteria.andTagEqualTo(map.getKey());
                Field field = cls.getDeclaredField(map.getValue());
                field.setAccessible(true);
                List<Dict> result = this.dictMapper.selectDataValueByExample(dictExample);
                field.set(placeCommonDataVo,result);
            }
        }
        catch (Exception e){
            e.printStackTrace();
            APIResponse.returnFail(ApiResponseEnum.INTERNAL_ERROR);
        }

        CagetoryExample cagetoryExample = new CagetoryExample();
        CagetoryExample.Criteria cagetoryCriteria = cagetoryExample.createCriteria();
        cagetoryCriteria.andIdLessThanOrEqualTo(4);
        List<Cagetory> cagetoryList = this.cagetoryMapper.selectNameByExample(cagetoryExample);
        placeCommonDataVo.setCagetory_list(cagetoryList);

        return APIResponse.returnSuccess(placeCommonDataVo);
    }

    /**
     * 根据场馆ID获取场馆附属信息
     * @param placeId
     * @return
     */
    public TbBizPlaceAttach getBizPlaceAttach(Integer placeId) {
        TbBizPlaceAttachExample example= new TbBizPlaceAttachExample();
        TbBizPlaceAttachExample.Criteria criterial = example.createCriteria();
        if (placeId != null){
            criterial.andPlaceIdEqualTo(placeId);
        }
        List<TbBizPlaceAttach> result = tbBizPlaceAttachMapper.selectByExample(example);
        if (result != null && result.size() > 0){
            return result.get(0);
        } else {
            return null;
        }
    }

    /**
     * 根据场馆Id获取场地列表
     * @param placeId
     * @return
     */
    public List<TbBizPlaceUnit> getPlaceUnitByPlaceId(Integer placeId) {
        TbBizPlaceUnitExample example=new TbBizPlaceUnitExample();
        TbBizPlaceUnitExample.Criteria criterial = example.createCriteria();
        if(placeId != null){
            criterial.andPlaceIdEqualTo(placeId);
            criterial.andIsDeleteEqualTo(Constants.BYTE_ZERO);
        }
        List<TbBizPlaceUnit> result = placeUnitMapper.selectByExample(example);
        return  result;
    }


    /**
     * 获取场馆场地片数
     * @param placeId
     * @return
     */
    public int countPlaceUnitByPlaceId(Integer placeId) {
        TbBizPlaceUnitExample example=new TbBizPlaceUnitExample();
        TbBizPlaceUnitExample.Criteria criterial = example.createCriteria();
        if(placeId != null){
            criterial.andPlaceIdEqualTo(placeId);
            criterial.andIsDeleteEqualTo(Constants.BYTE_ZERO);
        }
        return placeUnitMapper.countByExample(example);
    }

    /**
     * 计算周黄金时长
     * @param placeId
     * @return
     */
    public Double computePrimeTimeByWeek(Integer placeId) {
        Double resultTime = 0d;
        List<VPlaceTime> result = vPlaceMapper.getPrimeTimeByPlaceId(placeId, null);
        for (VPlaceTime p : result){
            if (PlaceConstants.DICT_TIME_TIME_WORKDAY.equals(p.getTimeType())){
                resultTime += p.getLengthOfTime() * 5;
            } else if (PlaceConstants.DICT_TIME_TIME_WEEKEND.equals(p.getTimeType())){
                resultTime += p.getLengthOfTime() * 2;
            }
        }
        return resultTime;
    }

    /**
     * 计算周销售时长
     * @param placeAttach
     * @param unitNum
     * @return
     */
    public Double computeSaleTimeInWeek(TbBizPlaceAttach placeAttach, Integer unitNum) {
        Double lengthOfTime = 0d;
        List<VPlaceTime> result = vPlaceMapper.getBusinessTimeByPlaceId(placeAttach.getPlaceId());
        if (result != null && result.size() > 0){
            VPlaceTime placeTime = result.get(0);
            lengthOfTime += placeTime.getLengthOfTime() * 5;
            lengthOfTime += placeTime.getLengthOfTimeHoliday() * 2;
            lengthOfTime *= unitNum;
        }

        return lengthOfTime;
    }

    /**
     * 获取片场黄金时间统计的javabean
     * @param tbBizPlaceUnitTimeList   每个片场下的时间列表（包含营业时间和黄金时间）
     * @return
     */
    public  TbBizPlaceUnitStatistics getPlaceUnitStatisticsBean(List<TbBizPlaceUnitTime> tbBizPlaceUnitTimeList) {
        try {
            String week_hour_num = "00:00:00";
            String week_prime_hour_num = "00:00:00";
            TbBizPlaceUnitStatistics tbBizPlaceUnitStatistics = new TbBizPlaceUnitStatistics();
            if (null != tbBizPlaceUnitTimeList && tbBizPlaceUnitTimeList.size() > 0){
                tbBizPlaceUnitStatistics.setPlaceId(tbBizPlaceUnitTimeList.get(0).getPlaceId());
                tbBizPlaceUnitStatistics.setPlaceUnitId(tbBizPlaceUnitTimeList.get(0).getPlaceUnitId());
            }
            Map<Integer,String> timeMap = new HashMap<Integer, String>();
            timeMap.put(1,"00:00:00");
            timeMap.put(0, "00:00:00");
            if(tbBizPlaceUnitTimeList != null && tbBizPlaceUnitTimeList.size() > 0){
                for (TbBizPlaceUnitTime tbBizPlaceUnitTime : tbBizPlaceUnitTimeList){
                    String countTime = TimeUtils.subtractTime(tbBizPlaceUnitTime.getStartTime(), tbBizPlaceUnitTime.getEndTime());
                    String tempTime = TimeUtils.addTime(timeMap.get(tbBizPlaceUnitTime.getIsPrimeTime()), countTime);
                    timeMap.put(tbBizPlaceUnitTime.getIsPrimeTime(),tempTime);
                }

                week_prime_hour_num = timeMap.get(1);
                week_hour_num = timeMap.get(0);
                week_hour_num = TimeUtils.addTime(week_hour_num, week_prime_hour_num);
                double weekPrimeHourNum = TimeUtils.timeToHour(week_prime_hour_num);
                double weekHourNum = TimeUtils.timeToHour(week_hour_num);
                tbBizPlaceUnitStatistics.setWeekPrimeHourNum(BigDecimal.valueOf(weekPrimeHourNum));
                tbBizPlaceUnitStatistics.setWeekHourNum(BigDecimal.valueOf(weekHourNum));
            }

            return tbBizPlaceUnitStatistics;
        }
        catch (ParseException e){
            e.printStackTrace();
        }
        return null;
    }



    /*public APIResponse importPlaceDataValidator(PlaceFromDataVo placeFromDataVo) {
        TbBizPlaceExample tbBizPlaceExample = new TbBizPlaceExample();
        TbBizPlaceExample.Criteria tbBizPlaceCriteria = tbBizPlaceExample.createCriteria();
        tbBizPlaceCriteria.andNameEqualTo(placeFromDataVo.getName());
        tbBizPlaceCriteria.andIsDeleteEqualTo(Constants.IS_NO);
        List<TbBizPlace> tbBizPlaceList =  placeMapper.selectByExample(tbBizPlaceExample);

        if(tbBizPlaceList != null && tbBizPlaceList.size() > 0){
            return APIResponse.returnFail("场馆名称已经存在!");
        }

        APIResponse apiResponse = ValidatorUtils.lengthValidateMustInput(placeFromDataVo.getName(), "场馆名称", 30);
        if( apiResponse != null ){
            return apiResponse;
        }

        apiResponse = ValidatorUtils.lengthValidate(placeFromDataVo.getName(), "场馆地址", 50);
        if( apiResponse != null ){
            return apiResponse;
        }

        apiResponse = ValidatorUtils.phone(placeFromDataVo.getPhone());
        if( apiResponse != null ){
            return apiResponse;
        }

        apiResponse = ValidatorUtils.isNumAndMustInput(String.valueOf(placeFromDataVo.getParkNum()), "停车场车位数", 10000);
        if( apiResponse != null ){
            return apiResponse;
        }

        apiResponse = ValidatorUtils.lengthValidate(placeFromDataVo.getOwnership(), "场馆所有权", 30);
        if( apiResponse != null ){
            return apiResponse;
        }

        apiResponse = ValidatorUtils.lengthValidate(placeFromDataVo.getComment(), "场馆描述", 200);
        if( apiResponse != null ){
            return apiResponse;
        }

        apiResponse = ValidatorUtils.isLat(String.valueOf(placeFromDataVo.getLat()));//纬度校验
        if( apiResponse != null ){
            return apiResponse;
        }

        apiResponse = ValidatorUtils.isLng(String.valueOf(placeFromDataVo.getLng()));//经度校验
        if( apiResponse != null ){
            return apiResponse;
        }

        apiResponse = ValidatorUtils.isYear(placeFromDataVo.getStartBusinessTime());//开业时间校验
        if( apiResponse != null ){
            return apiResponse;
        }

        apiResponse = ValidatorUtils.decimal((placeFromDataVo.getRemainTime()), "运营方经营年限");//运营方经营年限校验
        if( apiResponse != null ){
            return apiResponse;
        }


        apiResponse = ValidatorUtils.isTime(placeFromDataVo.getBusinessHourStart(), "工作日开始营业时间");
        if(apiResponse != null){
            return apiResponse;
        }

        apiResponse = ValidatorUtils.isTime(placeFromDataVo.getBusinessHourEnd(),"工作日结束营业时间");
        if(apiResponse != null){
            return apiResponse;
        }

        apiResponse = ValidatorUtils.isTime(placeFromDataVo.getHolidayBusinessHourStart(),"节假日开始营业时间");
        if(apiResponse != null){
            return apiResponse;
        }

        apiResponse = ValidatorUtils.isTime(placeFromDataVo.getHolidayBusinessHourEnd(),"节假日结束营业时间");
        if(apiResponse != null){
            return apiResponse;
        }


        if(placeFromDataVo.getBusinessHourStart().compareTo(placeFromDataVo.getBusinessHourEnd()) >= 0){
            return APIResponse.returnFail("工作日营业结束时间" +placeFromDataVo.getBusinessHourEnd() + "小于或开始时间" + placeFromDataVo.getBusinessHourStart());
        }

        if(placeFromDataVo.getHolidayBusinessHourStart().compareTo(placeFromDataVo.getHolidayBusinessHourEnd()) >= 0){
            return APIResponse.returnFail("节假日营业结束时间" +placeFromDataVo.getHolidayBusinessHourEnd() + "小于或开始时间" + placeFromDataVo.getHolidayBusinessHourStart());
        }

        if(placeFromDataVo.getPlace_attach_list() != null){
            if(placeFromDataVo.getPlace_attach_list().size() > 5){
                return APIResponse.returnFail("场地图片数量超过5张。");
            }
        }

        List<Integer> tbBizPlaceCategoryList = placeFromDataVo.getPlaceCategoryList();
        if(tbBizPlaceCategoryList == null || tbBizPlaceCategoryList.size() == 0){
            return APIResponse.returnFail("场地类型为空。");
        }

        List<TbBizPlaceUnitVo> bBizPlaceUnitVoList = placeFromDataVo.getPlace_unit_list();
        if(bBizPlaceUnitVoList != null && bBizPlaceUnitVoList.size() > 0){
            return  importPlaceUnitDataValidator(bBizPlaceUnitVoList);
        }
        return  null;
    }

   /* @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse importPlaceUnitData(PlaceFromDataVo placeFromDataVo) {
        APIResponse apiResponse = new APIResponse();

        List<TbBizPlaceUnitVo> list = placeFromDataVo.getPlace_unit_list();
        if(list == null || list.size() == 0){
            return APIResponse.returnFail( "场地为空！");
        }
        apiResponse =  importPlaceUnitDataValidator(list);
        if(apiResponse != null){
            return apiResponse;
        }

        for(TbBizPlaceUnitVo tbBizPlaceUnitVo : list){
            TbBizPlaceUnitExample tbBizPlaceUnitExample = new TbBizPlaceUnitExample();
            TbBizPlaceUnitExample.Criteria tbBizPlaceUnitCriteria = tbBizPlaceUnitExample.createCriteria();
            tbBizPlaceUnitCriteria.andNameEqualTo(tbBizPlaceUnitVo.getName());
            tbBizPlaceUnitCriteria.andPlaceIdEqualTo(placeFromDataVo.getPlaceId());
            tbBizPlaceUnitCriteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
            tbBizPlaceUnitCriteria.andCategoryIdEqualTo(tbBizPlaceUnitVo.getCategoryId());
            List<TbBizPlaceUnit> tbBizPlaceUnitList = placeUnitMapper.selectByExample(tbBizPlaceUnitExample);
            if(tbBizPlaceUnitList != null && tbBizPlaceUnitList.size() > 0){
                return APIResponse.returnFail( "场地名称：" + tbBizPlaceUnitVo.getName() + "已经存在！");
            }

        }

        TbBizPlaceExample tbBizPlaceExample = new TbBizPlaceExample();
        TbBizPlaceExample.Criteria tbBizPlaceCriteria = tbBizPlaceExample.createCriteria();
        tbBizPlaceCriteria.andIdEqualTo(placeFromDataVo.getPlaceId());
        tbBizPlaceCriteria.andIsDeleteEqualTo(Constants.IS_NO);
        List<TbBizPlace> tbBizPlaceList = placeMapper.selectByExample(tbBizPlaceExample);
        if (tbBizPlaceList.isEmpty()){
            return APIResponse.returnFail("不存在该场馆信息！");
        }
        TbBizPlace tbBizPlace = tbBizPlaceList.get(0);
        String placeName = tbBizPlace.getName();

        Boolean isPrimeTime = false;
        for (TbBizPlaceUnitVo tbBizPlaceUnitVo : list){
            TbBizPlaceUnit tbBizPlaceUnit = new TbBizPlaceUnit();
            tbBizPlaceUnit.setName(tbBizPlaceUnitVo.getName());
            tbBizPlaceUnit.setPlaceId(placeFromDataVo.getPlaceId());
            tbBizPlaceUnit.setCategoryId(tbBizPlaceUnitVo.getCategoryId());
            tbBizPlaceUnit.setExtendName(placeName + "_" + tbBizPlaceUnit.getName() + "_" + getCommonDataCache("cagetory"+tbBizPlaceUnit.getCategoryId().toString())+"场");
            Short sh = 0;
            tbBizPlaceUnit.setPriority(sh);
            tbBizPlaceUnit.setComment(tbBizPlaceUnitVo.getComment());
            tbBizPlaceUnit.setFieldScale(tbBizPlaceUnitVo.getFieldScale());
            tbBizPlaceUnit.setLightType(tbBizPlaceUnitVo.getLightType());
            tbBizPlaceUnit.setPlaceUnitTexture(tbBizPlaceUnitVo.getPlaceUnitTexture());
            tbBizPlaceUnit.setPlaceUnitType(tbBizPlaceUnitVo.getPlaceUnitType());
            tbBizPlaceUnit.setIsDelete(Constants.IS_DELETE_FALSE);
            tbBizPlaceUnit.setIsSign(Constants.BYTE_ZERO);

            placeUnitMapper.insert(tbBizPlaceUnit);//插入片场表

            //插入营业时间
            List<TbBizPlaceUnitTime> tbBizPlaceUnitTimes = tbBizPlaceUnitVo.getPlace_unit_time_list();
            for(TbBizPlaceUnitTime tbBizPlaceUnitTime:tbBizPlaceUnitTimes){
                if(tbBizPlaceUnitTime.getIsPrimeTime() == 1){
                    isPrimeTime = true;
                }
                String time_length = "00:00:00";
                try {
                    time_length = TimeUtils.subtractTime(tbBizPlaceUnitTime.getStartTime(), tbBizPlaceUnitTime.getEndTime());
                }
                catch (ParseException e){
                     e.printStackTrace();
                }
                tbBizPlaceUnitTime.setTimeLength(TimeUtils.timeToHour(time_length));
                tbBizPlaceUnitTime.setPlaceUnitId(tbBizPlaceUnit.getId());
                tbBizPlaceUnitTime.setPlaceId(placeFromDataVo.getPlaceId());
                tbBizPlaceUnitTimeMapper.insert(tbBizPlaceUnitTime);
            }

            TbBizPlaceUnitStatistics tbBizPlaceUnitStatistics = getPlaceUnitStatisticsBean(tbBizPlaceUnitTimes);
            tbBizPlaceUnitStatistics.setIsSign(Constants.BYTE_ZERO);
            tbBizPlaceUnitStatistics.setWeekSignHourNum(BigDecimal.ZERO);
            tbBizPlaceUnitStatistics.setWeekSignPrimeHourNum(BigDecimal.ZERO);

            tbBizPlaceUnitStatisticsMapper.insert(tbBizPlaceUnitStatistics); //插入片场黄金时间统计表
        }
        //更新场馆黄金时间属性
        if(isPrimeTime){
            TbBizPlaceAttach tbBizPlaceAttach = new TbBizPlaceAttach();
            tbBizPlaceAttach.setIsContainPrimeTime((byte) 1);
            TbBizPlaceAttachExample tbBizPlaceAttachExample = new TbBizPlaceAttachExample();
            TbBizPlaceAttachExample.Criteria tbBizPlaceAttachCriteria = tbBizPlaceAttachExample.createCriteria();
            tbBizPlaceAttachCriteria.andPlaceIdEqualTo(placeFromDataVo.getPlaceId());
            tbBizPlaceAttachMapper.updateByExampleSelective(tbBizPlaceAttach,tbBizPlaceAttachExample);
        }
        return APIResponse.returnSuccess("录入完成");
    }*/

    /*public APIResponse importPlaceUnitDataValidator( List<TbBizPlaceUnitVo> bBizPlaceUnitVoList) {
        HashMap<String,String> result = new HashMap<String,String>();
        APIResponse apiResponse = null;
        for (TbBizPlaceUnitVo tbBizPlaceUnitVo : bBizPlaceUnitVoList){//遍历场地列表
            apiResponse = ValidatorUtils.lengthValidateMustInput(tbBizPlaceUnitVo.getName(), "场地名称", 20);

            if( apiResponse != null ){
                return apiResponse;
            }

            if(result.containsKey(tbBizPlaceUnitVo.getName())){
                return APIResponse.returnFail("场地名称" + tbBizPlaceUnitVo.getName() + "重复");
            }
            else{
                result.put(tbBizPlaceUnitVo.getName(),"场地名称");
            }

            apiResponse = ValidatorUtils.lengthValidate(tbBizPlaceUnitVo.getComment(), "场地备注", 50);

            if( apiResponse != null ){
                return apiResponse;
            }

            List<TbBizPlaceUnitTime> tbBizPlaceUnitTimeList = tbBizPlaceUnitVo.getPlace_unit_time_list();

            if(tbBizPlaceUnitTimeList != null && tbBizPlaceUnitTimeList.size() > 0){
                for (int i = 0; i < tbBizPlaceUnitTimeList.size(); i++) {
                    TbBizPlaceUnitTime tbBizPlaceUnitTime = tbBizPlaceUnitTimeList.get(i);
                    String EndTime = tbBizPlaceUnitTime.getEndTime();
                    String StartTime = tbBizPlaceUnitTime.getStartTime();

                    apiResponse = ValidatorUtils.isTime(StartTime,iBaseService.getCommonDataCache(tbBizPlaceUnitTime.getTimeType()) +"的开始时间" + StartTime);
                    if(apiResponse != null){
                        return apiResponse;
                    }

                    apiResponse = ValidatorUtils.isTime(EndTime,iBaseService.getCommonDataCache(tbBizPlaceUnitTime.getTimeType()) + "的结束时间" + EndTime);
                    if(apiResponse != null){
                        return apiResponse;
                    }

                    if(StartTime.compareTo(EndTime) >= 0){
                        return APIResponse.returnFail(iBaseService.getCommonDataCache(tbBizPlaceUnitTime.getTimeType())+"的结束时间" +EndTime + "小于或等于开始时间" + StartTime);
                    }

                }

                for (int i = 0; i < tbBizPlaceUnitTimeList.size()-1; i++) {
                    TbBizPlaceUnitTime tbBizPlaceUnitTime1 = tbBizPlaceUnitTimeList.get(i);
                    for (int j = i + 1; j < tbBizPlaceUnitTimeList.size(); j++) {
                        TbBizPlaceUnitTime tbBizPlaceUnitTime2 = tbBizPlaceUnitTimeList.get(j);
                        if(tbBizPlaceUnitTime1.getTimeType().equals(tbBizPlaceUnitTime2.getTimeType())){
                            if(TimeUtils.isOverLaps(tbBizPlaceUnitTime1.getStartTime(), tbBizPlaceUnitTime1.getEndTime(), tbBizPlaceUnitTime2.getStartTime(), tbBizPlaceUnitTime2.getEndTime(), "HH:mm") ){
                                return APIResponse.returnFail(iBaseService.getCommonDataCache(tbBizPlaceUnitTime1.getTimeType())+"的时段"+ tbBizPlaceUnitTime1.getStartTime()+ "-"+ tbBizPlaceUnitTime1.getEndTime()+"和"+ tbBizPlaceUnitTime2.getStartTime()+ "-"+ tbBizPlaceUnitTime2.getEndTime()+"有重叠!");
                            }
                        }
                    }
                }

            }
        }
        return null;
    }




    /**
     * 此处接收的字符串为“yyyy-MM-dd HH:mm”或 “HH:mm”
     * @param strDate
     * @return
     */
    private java.util.Date changeStrDateToTime(String strDate) {
        Calendar calTime = Calendar.getInstance();
        if (strDate != null){
            if (strDate.length() == 5){
                strDate = DateUtils.convertDateFormatToString(calTime.getTime()) + " " + strDate;
            }
            if (strDate.length() == 16){
                Date date = strToDate(strDate, DateUtils.DAY_DATE_FORMAT_HH_MM);
                calTime.setTime(date);
            }
            return calTime.getTime();
        } else {
            return null;
        }
    }


    /**
     * 根据提供商id查询提供商信息
     * @param supplierId
     * @return
     */
    public TbSupplier getSupplierDataById(Integer supplierId) {
        TbSupplier supplier = tbSupplierMapper.selectByPrimaryKey(supplierId);
        return supplier;
    }




    /**
     * 获取省信息
     * @return
     */
    @Override
    public APIResponse getProvinceData() {
        TbProvinceExample tbProvinceExample = new TbProvinceExample();
        TbProvinceExample.Criteria tbProvinceCriteria  = tbProvinceExample.createCriteria();
        tbProvinceCriteria.andIsEnableEqualTo(1);
        tbProvinceExample.setOrderByClause("priority");
        List<TbProvince> list = tbProvinceMapper.selectNameByExample(tbProvinceExample);
        return APIResponse.returnSuccess(list);
    }

    /**
     * 获取省下的城市信息
     * @param provinceId
     * @param isEnabled
     * @return
     */
    @Override
    public APIResponse getCityData(Integer provinceId, boolean isEnabled) {
        try {
            TbCityExample tbCityExample = new TbCityExample();
            TbCityExample.Criteria tbCityCriteria = tbCityExample.createCriteria();
            if (provinceId != null && provinceId > 0){
                tbCityCriteria.andProvinceIdEqualTo(provinceId);
            }
            if (isEnabled){
                tbCityCriteria.andIsEnableEqualTo(1);
            }
            tbCityExample.setOrderByClause("priority");
            List<TbCity> list = tbCityMapper.selectNameByExample(tbCityExample);
            return APIResponse.returnSuccess(list);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return APIResponse.returnFail(ApiResponseEnum.RESOURCE_NOT_FOUND);

    }

    public TbCity getCityDataById(Integer cityId) {
        TbCityExample tbCityExample = new TbCityExample();
        TbCityExample.Criteria tbCityCriteria = tbCityExample.createCriteria();
        tbCityCriteria.andIdEqualTo(cityId);
        List<TbCity> list = tbCityMapper.selectNameByExample(tbCityExample);
        return list.get(0);
    }

    public RespCity getRespCityById(Integer cityId) {
        TbCity tbCity = tbCityMapper.selectByPrimaryKey(cityId);
        if (!org.springframework.util.StringUtils.isEmpty(tbCity)) {
            RespCity respCity = new RespCity();
            try {
                BeanUtils.copyProperties(respCity, tbCity);
                return respCity;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public List<RespCityInfos> getSysCityInfos() {
        //查询城市列表
        TbCityExample tbCityExample = new TbCityExample();
        tbCityExample.createCriteria().andIsActiveEqualTo(Constants.IS_ACTIVE)
                .andIsEnableEqualTo(Integer.parseInt(Constants.IS_EDITABLE_TRUE + ""));
        List<TbCity> cities = tbCityMapper.selectByExample(tbCityExample);
        if (CollectionUtils.isEmpty(cities)) {
            return null;
        }
        //查询地区列表
        TbDistrictExample tbDistrictExample = new TbDistrictExample();
        List<TbDistrict> tbDistricts = tbDistrictMapper.selectByExample(tbDistrictExample);
        if (CollectionUtils.isEmpty(tbDistricts)) {
            return null;
        }
        List<RespCityInfos> respCityInfos = new ArrayList<RespCityInfos>();
        for (TbCity tbCity : cities) {
            RespCityInfos respCityInfo = new RespCityInfos();
            RespCity respCity = new RespCity();
            org.springframework.beans.BeanUtils.copyProperties(tbCity, respCity);
            //获取城市对应区列表
            List<RespDistrict> respDistrictList = new ArrayList<>();

            for (TbDistrict tbDistrict : tbDistricts) {
                if (tbCity.getId() == tbDistrict.getCityId().intValue()) {
                    RespDistrict respDistrict = new RespDistrict();
                    org.springframework.beans.BeanUtils.copyProperties(tbDistrict, respDistrict);
                    respDistrictList.add(respDistrict);
                } else {
                    continue;
                }
            }
            respCityInfo.setRespDistrict(respDistrictList);
            respCityInfo.setRespCity(respCity);
            respCityInfos.add(respCityInfo);
        }
        return respCityInfos;
    }

    @Override
    public APIResponse<?> isOrderUse(Integer uid, String placeOrderId, Integer type) {
        if (org.springframework.util.StringUtils.isEmpty(type)){
            return APIResponse.returnFail("订单顶用类型参数不能为空");
        }
        if (org.springframework.util.StringUtils.isEmpty(placeOrderId)){
            return APIResponse.returnFail("订单编号参数不能为空");
        }
        TbBizPlaceOrderExample example = new TbBizPlaceOrderExample();
        example.createCriteria().andUidEqualTo(uid).andPlaceOrderIdEqualTo(placeOrderId);
        TbBizPlaceOrder tbBizPlaceOrder = new TbBizPlaceOrder();
        tbBizPlaceOrder.setOrderUse(type.byteValue());
        int flag = this.tbBizPlaceOrderMapper.updateByExampleSelective(tbBizPlaceOrder, example);
        if(flag == 1){
            logger.info("订单使用成功：用户id:{}，订单号：{}，类型：{}",uid,placeOrderId,type);
            return APIResponse.returnSuccess(flag == 1);
        }
        else {
            logger.warn("订单使用失败：用户id:{}，订单号：{}，类型：{}", uid, placeOrderId, type);
        }

        return APIResponse.returnFail("订单使用失败");
    }

    @Override
    public APIResponse selectPlaceByName(String placeName) {
        TbBizPlaceExample tbBizPlaceExample = new TbBizPlaceExample();
        TbBizPlaceExample.Criteria criteria = tbBizPlaceExample.createCriteria();
        criteria.andNameLike("%" + placeName + "%");
        criteria.andSourceIdEqualTo(2);
        criteria.andIsSignEqualTo(Constants.BYTE_ONE);
        criteria.andIsDeleteEqualTo(0);
        return APIResponse.returnSuccess(placeMapper.selectByExample(tbBizPlaceExample));
    }

    @Override
    public APIResponse getPlaceUnitList(Integer placeId) {
        TbBizPlaceUnitExample tbBizPlaceUnitExample = new TbBizPlaceUnitExample();
        TbBizPlaceUnitExample.Criteria criteria = tbBizPlaceUnitExample.createCriteria();
        criteria.andPlaceIdEqualTo(placeId);
        return APIResponse.returnSuccess(placeUnitMapper.selectByExample(tbBizPlaceUnitExample));

    }

    @Override
    public Map<Integer, PlaceDubboProvider> getPlaceByTerm(List placeIds, Double lat, Double lng) {
        Map<Integer, PlaceDubboProvider> placeMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(placeIds)){
            PlaceDubboProvider placeDubboProvider = null;
            for (int i=0; i<placeIds.size(); i++){
                Integer placeId = (Integer) placeIds.get(i);
                        placeDubboProvider = new PlaceDubboProvider();
                        TbBizPlace tbBizPlace = placeMapper.selectByPrimaryKey(placeId);
                        if (null != tbBizPlace){
                            placeDubboProvider.setCityId(tbBizPlace.getCityId());
                            placeDubboProvider.setName(tbBizPlace.getName());
                            placeDubboProvider.setLat(tbBizPlace.getLat());
                            placeDubboProvider.setLng(tbBizPlace.getLng());
                            placeDubboProvider.setAddress(tbBizPlace.getAddress());
                            placeDubboProvider.setDistrictId(tbBizPlace.getDistrictId());
                            placeDubboProvider.setPlaceId(tbBizPlace.getId());
                            TbCity tbCity = this.getCityDataById(tbBizPlace.getCityId());
                            if (!org.springframework.util.StringUtils.isEmpty(tbCity)) {
                                placeDubboProvider.setCityName(tbCity.getName());
                            }
                            TbDistrict tbDistrict = tbDistrictMapper.selectByPrimaryKey(tbBizPlace.getDistrictId());
                            if (!org.springframework.util.StringUtils.isEmpty(tbDistrict)){
                                placeDubboProvider.setDestrictName(tbDistrict.getName());
                            }
                            placeDubboProvider.setBizPlaceUrl(tbBizPlace.getBizPlaceUrl());
                            placeDubboProvider.setPlaceScore(tbBizPlace.getPlaceScore());
                            placeDubboProvider.setCommentCount(tbBizPlace.getCommentCount());

                        	//默认取有销售计划的栏目
                            Integer categoryId  = getPlaceSalePlanCategory(tbBizPlace.getId());
                            if (categoryId == null) {
                            	 TbBizPlaceCategoryExample tbBizPlaceCategoryExample = new TbBizPlaceCategoryExample();
                                 tbBizPlaceCategoryExample.createCriteria().andPlaceIdEqualTo(tbBizPlace.getId());
                                 List<TbBizPlaceCategory> tbBizPlaceCategorys
                                         = tbBizPlaceCategoryMapper.selectByExample(tbBizPlaceCategoryExample);
                                 if (CollectionUtils.isNotEmpty(tbBizPlaceCategorys)){
                                    categoryId = tbBizPlaceCategorys.get(0).getCategoryId();
                                 }
                            }
                            placeDubboProvider.setCategoryId(categoryId);
                           

                            TbBizPlaceAttachExample example = new TbBizPlaceAttachExample();
                            example.createCriteria().andPlaceIdEqualTo(placeId);
                            List<TbBizPlaceAttach> tbBizPlaceAttachs = tbBizPlaceAttachMapper.selectByExample(example);
                            if (CollectionUtils.isNotEmpty(tbBizPlaceAttachs)){
                                placeDubboProvider.setIsVip(tbBizPlaceAttachs.get(0).getIsVip());
                            }
                            lat = Double.isNaN(lat) ? 0D : lat;
                            lng = Double.isNaN(lng) ? 0D :lng;
                            if (lat == 0 && lng == 0){
                                placeDubboProvider.setDistance(0D);
                            }
                            placeDubboProvider.setDistance(DistanceUtil.getDistance(lng, lat, tbBizPlace.getLng(), tbBizPlace.getLat()) * 1000);
                        }
                        placeMap.put(placeId, placeDubboProvider);
                    }

        }
        return placeMap;
    }
    
    
    
    private Integer getPlaceSalePlanCategory(Integer placeId){
    	   TbBizPlaceUnitSalePlanExample tbBizPlaceUnitSalePlanExample = new TbBizPlaceUnitSalePlanExample();
           TbBizPlaceUnitSalePlanExample.Criteria tbBizPlaceUnitSalePlanCriteria =
                   tbBizPlaceUnitSalePlanExample.createCriteria();
           tbBizPlaceUnitSalePlanCriteria.andPlaceIdEqualTo(placeId);
           //tbBizPlaceUnitSalePlanCriteria.andCategoryIdEqualTo(categoryId);
           
           tbBizPlaceUnitSalePlanCriteria.andDateGreaterThanOrEqualTo(DateUtils.getDateFormat(new Date()));
          
           tbBizPlaceUnitSalePlanCriteria.andIsEffectiveEqualTo(Constants.YES);
           tbBizPlaceUnitSalePlanCriteria.andUnitTypeEqualTo(PlaceConstants.UNIT_TYPE_BASE);      // 只选择基本场馆
           
           tbBizPlaceUnitSalePlanExample.setPage(Page.createPage(10, 0, 1));
           List<TbBizPlaceUnitSalePlan> placeUnitSalePlanList =  tbBizPlaceUnitSalePlanMapper.selectByExample(tbBizPlaceUnitSalePlanExample);
           
    	return !placeUnitSalePlanList.isEmpty() ? placeUnitSalePlanList.get(0).getCategoryId() : null;
    }

    /**
     * 获取片场信息
     * @param placeId
     * @return
     *//*
    @Override
    public APIResponse getPlaceUnitData(int placeId,int placeUnitId) {
        TbBizPlaceUnitExample tbBizPlaceUnitExample = new TbBizPlaceUnitExample();
        TbBizPlaceUnitExample.Criteria tbBizPlaceUnitCriteria = tbBizPlaceUnitExample.createCriteria();
        if(placeId > 0){
            tbBizPlaceUnitCriteria.andPlaceIdEqualTo(placeId);
            tbBizPlaceUnitCriteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
        }
        if (placeUnitId > 0){
            tbBizPlaceUnitCriteria.andIdEqualTo(placeUnitId);
            tbBizPlaceUnitCriteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
        }
        if (placeId <=0 && placeUnitId <= 0){
           return APIResponse.returnFail("placeId或者placeUnitId至少必须填写一个大于0的参数。");
        }

        List<TbBizPlaceUnit> tbBizPlaceUnitList = placeUnitMapper.selectByExample(tbBizPlaceUnitExample);
        if(tbBizPlaceUnitList != null && tbBizPlaceUnitList.size() > 0){
            for (TbBizPlaceUnit tbBizPlaceUnit : tbBizPlaceUnitList){
                tbBizPlaceUnit.setPlaceUnitType(getCommonDataCache(tbBizPlaceUnit.getPlaceUnitType()));
                tbBizPlaceUnit.setPlaceUnitTexture(getCommonDataCache(tbBizPlaceUnit.getPlaceUnitTexture()));
                tbBizPlaceUnit.setLightType(getCommonDataCache(tbBizPlaceUnit.getLightType()));
                tbBizPlaceUnit.setFieldScale(getCommonDataCache(tbBizPlaceUnit.getFieldScale()));
            }
        }

        return APIResponse.returnSuccess(tbBizPlaceUnitList);
    }*/

    /**
     * 获取片场详情接口
     * @param placeUnitId
     * @return
     * @throws Exception
     */
    @Deprecated
    public APIResponse getPlaceUnitDetailData(int placeUnitId)  throws Exception{
        TbBizPlaceUnitExample tbBizPlaceUnitExample = new TbBizPlaceUnitExample();
        TbBizPlaceUnitExample.Criteria tbBizPlaceUnitCriteria = tbBizPlaceUnitExample.createCriteria();
        tbBizPlaceUnitCriteria.andIdEqualTo(placeUnitId);
        tbBizPlaceUnitCriteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
        List<TbBizPlaceUnit> tbBizPlaceUnitList = placeUnitMapper.selectByExample(tbBizPlaceUnitExample);
        TbBizPlaceUnitVo tbBizPlaceUnitVo = new TbBizPlaceUnitVo();

        if(tbBizPlaceUnitList != null && tbBizPlaceUnitList.size() > 0){
            for (TbBizPlaceUnit tbBizPlaceUnit : tbBizPlaceUnitList){
                BeanUtils.copyProperties(tbBizPlaceUnitVo, tbBizPlaceUnit);
                TbBizPlaceUnitTimeExample tbBizPlaceUnitTimeExample = new TbBizPlaceUnitTimeExample();
                TbBizPlaceUnitTimeExample.Criteria tbBizPlaceUnitTimeCriteria = tbBizPlaceUnitTimeExample.createCriteria();
                tbBizPlaceUnitTimeCriteria.andPlaceUnitIdEqualTo(tbBizPlaceUnit.getId());
                tbBizPlaceUnitTimeCriteria.andPlaceIdEqualTo(tbBizPlaceUnit.getPlaceId());
                List<TbBizPlaceUnitTime> tbBizPlaceUnitTimeList = tbBizPlaceUnitTimeMapper.selectByExample(tbBizPlaceUnitTimeExample);
                tbBizPlaceUnitVo.setPlace_unit_time_list(tbBizPlaceUnitTimeList);
            }
        }
        return  APIResponse.returnSuccess(tbBizPlaceUnitVo);
    }



    /**
     * 根据type获取字典类型
     * @param type
     * @return
     */

    public Map<String,String> getDictDisplayName(String type){
        HashMap<String,String> map = new HashMap<String,String>();
        DictExample dictExample = new DictExample();
        DictExample.Criteria dictCriteria = dictExample.createCriteria();
        dictCriteria.andTagGroupEqualTo(type);
        List<Dict> result = this.dictMapper.selectDataValueByExample(dictExample);
        for (Dict dict : result){
            map.put(dict.getDataKey(), dict.getDataValue());
        }
        return map;
    }


    /**
     * 获取运动类别信息
     * @return
     */
        @Override
    public APIResponse getCategory() {
        CagetoryExample cagetoryExample = new CagetoryExample();
        CagetoryExample.Criteria cagetoryCriteria = cagetoryExample.createCriteria();
        cagetoryCriteria.andIsEnableEqualTo(Constants.BYTE_ONE);
        cagetoryExample.setOrderByClause("id");
        List<Cagetory> list = cagetoryMapper.selectNameByExample(cagetoryExample);
        return APIResponse.returnSuccess(list);
    }

    /**
     * 获取字典表配置信息
     * @param tag
     * @param datakey
     * @return
     */
    public String getDictDataValue(String tag , String datakey){
        DictExample dictExample = new DictExample();
        DictExample.Criteria dictCriteria = dictExample.createCriteria();
        dictCriteria.andTagEqualTo(tag);
        dictCriteria.andDataKeyEqualTo(datakey);
        List<Dict> result = this.dictMapper.selectDataValueByExample(dictExample);
        return result.get(0).getDataValue();
    }

    /*@Override
    public APIResponse<PageObj<List<RespBizPlaceBaseInfo>>> getUserCoolectPlaceInfos(Integer uid, Integer pageNum, Integer pageSize) {
        return this.apiPlaceService.collectList(uid,Constants.BEIJING_LNG,Constants.BEIJING_LAT, pageNum, pageSize);//经纬度默认给北京
    }*/

    @Override
    public void delUserPlaceCache(Integer uid, List placeIds) {
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(placeIds)){
            RedisKey rediskey = new RedisKey(RedisConstants.PLACE_FAMILY_KEY, uid + PlaceConstants.USER_COLLECT_REDIS_KEY);

            List<byte[]> bytes = new ArrayList<>();
            for (int i=0,len=placeIds.size(); i<len; i++) {
                bytes.add(placeIds.get(i).toString().getBytes());
            }
            this.placeRedisTemplate.hdel(rediskey.getRedisKey(), (byte[][]) bytes.toArray());
        }
    }


    /*@Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse updatePlaceUnitData(TbBizPlaceUnitVo tbBizPlaceUnitVo) {

        TbBizPlaceUnitExample tbBizPlaceUnitExample = new TbBizPlaceUnitExample();
        TbBizPlaceUnitExample.Criteria tbBizPlaceUnitCriteria = tbBizPlaceUnitExample.createCriteria();

        tbBizPlaceUnitCriteria.andPlaceIdEqualTo(tbBizPlaceUnitVo.getPlaceId());
        tbBizPlaceUnitCriteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
        tbBizPlaceUnitCriteria.andIdNotEqualTo(tbBizPlaceUnitVo.getId());
        tbBizPlaceUnitCriteria.andCategoryIdEqualTo(tbBizPlaceUnitVo.getCategoryId());
        List<TbBizPlaceUnit> tbBizPlaceUnitList = placeUnitMapper.selectByExample(tbBizPlaceUnitExample);
        if(tbBizPlaceUnitList !=null && tbBizPlaceUnitList.size()>0){
            for(TbBizPlaceUnit tbBizPlaceUnit : tbBizPlaceUnitList){
                if(tbBizPlaceUnit.getName().equals(tbBizPlaceUnitVo.getName())){
                    return APIResponse.returnFail("场地名已经存在！");
                }
            }
        }

        List<TbBizPlaceUnitTime> tbBizPlaceUnitTimes = tbBizPlaceUnitVo.getPlace_unit_time_list();
        if(tbBizPlaceUnitTimes != null && tbBizPlaceUnitTimes.size() > 0){
            List<TbBizPlaceUnitVo> bBizPlaceUnitVoList = new ArrayList<TbBizPlaceUnitVo>();
            bBizPlaceUnitVoList.add(tbBizPlaceUnitVo);
            APIResponse apiResponse = importPlaceUnitDataValidator(bBizPlaceUnitVoList);
            if(apiResponse != null){
                return apiResponse;
            }

            //在片场时间表中删除相关片场的所有时间
            TbBizPlaceUnitTimeExample tbBizPlaceUnitTimeExample = new TbBizPlaceUnitTimeExample();
            TbBizPlaceUnitTimeExample.Criteria tbBizPlaceUnitTimeCriteria = tbBizPlaceUnitTimeExample.createCriteria();
            tbBizPlaceUnitTimeCriteria.andPlaceIdEqualTo(tbBizPlaceUnitVo.getPlaceId());
            tbBizPlaceUnitTimeCriteria.andPlaceUnitIdEqualTo(tbBizPlaceUnitVo.getId());
            tbBizPlaceUnitTimeMapper.deleteByExample(tbBizPlaceUnitTimeExample);

            //重新插入片场时间
            boolean isPrimeTime = false;
            for(TbBizPlaceUnitTime tbBizPlaceUnitTime : tbBizPlaceUnitTimes){
                if(tbBizPlaceUnitTime.getIsPrimeTime() == 1){
                    isPrimeTime = true;
                }
                String time_length = "00:00:00";
                try {
                    time_length = TimeUtils.subtractTime(tbBizPlaceUnitTime.getStartTime(), tbBizPlaceUnitTime.getEndTime());
                }
                catch (ParseException e){
                    e.printStackTrace();
                }
                tbBizPlaceUnitTime.setTimeLength(TimeUtils.timeToHour(time_length));
                tbBizPlaceUnitTime.setPlaceUnitId(tbBizPlaceUnitVo.getId());
                tbBizPlaceUnitTime.setPlaceId(tbBizPlaceUnitVo.getPlaceId());
                tbBizPlaceUnitTimeMapper.insert(tbBizPlaceUnitTime);
            }

            TbBizPlaceUnitStatistics tbBizPlaceUnitStatistics = getPlaceUnitStatisticsBean(tbBizPlaceUnitTimes);
            tbBizPlaceUnitStatisticsMapper.updateByPrimaryKeySelective(tbBizPlaceUnitStatistics); //更新片场时间统计表

            //更新场馆黄金时间属性
            if(isPrimeTime){
                TbBizPlaceAttach tbBizPlaceAttach = new TbBizPlaceAttach();
                tbBizPlaceAttach.setIsContainPrimeTime((byte) 1);
                TbBizPlaceAttachExample tbBizPlaceAttachExample = new TbBizPlaceAttachExample();
                TbBizPlaceAttachExample.Criteria tbBizPlaceAttachCriteria = tbBizPlaceAttachExample.createCriteria();
                tbBizPlaceAttachCriteria.andPlaceIdEqualTo(tbBizPlaceUnitVo.getPlaceId());
                tbBizPlaceAttachMapper.updateByExampleSelective(tbBizPlaceAttach,tbBizPlaceAttachExample);
            }

        }
        TbBizPlaceUnit tbBizPlaceUnit = new TbBizPlaceUnit();
        tbBizPlaceUnit.setName(tbBizPlaceUnitVo.getName());
        tbBizPlaceUnit.setId(tbBizPlaceUnitVo.getId());
        tbBizPlaceUnit.setPlaceId(tbBizPlaceUnitVo.getPlaceId());
        tbBizPlaceUnit.setCategoryId(tbBizPlaceUnitVo.getCategoryId());
        tbBizPlaceUnit.setComment(tbBizPlaceUnitVo.getComment());
        tbBizPlaceUnit.setFieldScale(tbBizPlaceUnitVo.getFieldScale());
        tbBizPlaceUnit.setLightType(tbBizPlaceUnitVo.getLightType());
        tbBizPlaceUnit.setPlaceUnitTexture(tbBizPlaceUnitVo.getPlaceUnitTexture());
        tbBizPlaceUnit.setPlaceUnitType(tbBizPlaceUnitVo.getPlaceUnitType());
        placeUnitMapper.updateByPrimaryKeySelective(tbBizPlaceUnit);
        return APIResponse.returnSuccess();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse updatePlaceData(PlaceFromDataVo placeFromDataVo) {
        List<TbPlaceImage> tbPlaceImageList = placeFromDataVo.getPlace_attach_list();
        TbBizPlaceExample tbBizPlaceExample = new TbBizPlaceExample();
        TbBizPlaceExample.Criteria tbBizPlaceCriteria = tbBizPlaceExample.createCriteria();
        tbBizPlaceCriteria.andNameEqualTo(placeFromDataVo.getName());
        tbBizPlaceCriteria.andIsDeleteEqualTo(Constants.IS_NO);
        tbBizPlaceCriteria.andIdNotEqualTo(placeFromDataVo.getId());
        List<TbBizPlace> tbBizPlaceList =  placeMapper.selectByExample(tbBizPlaceExample);
        if(tbBizPlaceList != null && tbBizPlaceList.size() > 0){
            return APIResponse.returnFail("场馆名称已经存在!");
        }

        TbBizPlace tbBizPlace = new TbBizPlace();
        tbBizPlace.setId(placeFromDataVo.getId());
        tbBizPlace.setName(placeFromDataVo.getName());
        tbBizPlace.setCityId(placeFromDataVo.getCityId());
        tbBizPlace.setAddress(placeFromDataVo.getAddress());
        tbBizPlace.setComment(placeFromDataVo.getComment());
        tbBizPlace.setDistrictId(placeFromDataVo.getDistrictId());
        tbBizPlace.setProvinceId(placeFromDataVo.getProvinceId());
        tbBizPlace.setContact(placeFromDataVo.getContact());
        tbBizPlace.setLat(placeFromDataVo.getLat());
        tbBizPlace.setLng(placeFromDataVo.getLng());
        tbBizPlace.setSupplierId(placeFromDataVo.getSupplierId());
        tbBizPlace.setPhone(placeFromDataVo.getPhone());
        if(tbPlaceImageList != null && tbPlaceImageList.size() > 0){
            tbBizPlace.setBizPlaceUrl(tbPlaceImageList.get(0).getUrl());
        }
        placeMapper.updateByPrimaryKeySelective(tbBizPlace);



        TbBizPlaceAttach tbBizPlaceAttach = new TbBizPlaceAttach();
        tbBizPlaceAttach.setPlaceId(placeFromDataVo.getId());
        tbBizPlaceAttach.setIsPrimeLocation(placeFromDataVo.getIsPrimeLocation());
        tbBizPlaceAttach.setIsContainPrimeTime(placeFromDataVo.getIsContainPrimeTime());
        tbBizPlaceAttach.setManageType(placeFromDataVo.getManageType());
        tbBizPlaceAttach.setPlaceLevel(placeFromDataVo.getPlaceLevel());
        tbBizPlaceAttach.setStartBusinessTime(placeFromDataVo.getStartBusinessTime());
        tbBizPlaceAttach.setRemainTime(placeFromDataVo.getRemainTime());
        tbBizPlaceAttach.setIsContainBath(placeFromDataVo.getIsContainBath());
        tbBizPlaceAttach.setIsContainWater(placeFromDataVo.getIsContainWater());
        tbBizPlaceAttach.setIsContainWifi(placeFromDataVo.getIsContainWifi());
        tbBizPlaceAttach.setIsContainHeating(placeFromDataVo.getIsContainHeating());
        tbBizPlaceAttach.setIsContainAc(placeFromDataVo.getIsContainAc());
        tbBizPlaceAttach.setIsContainAdArea(placeFromDataVo.getIsContainAdArea());
        tbBizPlaceAttach.setParkNum(placeFromDataVo.getParkNum());
        tbBizPlaceAttach.setIsParkFree(placeFromDataVo.getIsParkFree());
        tbBizPlaceAttach.setOwnership(placeFromDataVo.getOwnership()); //场馆所有权
        tbBizPlaceAttach.setBusinessHourStart(placeFromDataVo.getBusinessHourStart());
        tbBizPlaceAttach.setBusinessHourEnd(placeFromDataVo.getBusinessHourEnd());
        tbBizPlaceAttach.setHolidayBusinessHourStart(placeFromDataVo.getHolidayBusinessHourStart());
        tbBizPlaceAttach.setHolidayBusinessHourEnd(placeFromDataVo.getHolidayBusinessHourEnd());

        tbBizPlaceAttach.setIsVip(placeFromDataVo.getIsVip());
        tbBizPlaceAttach.setIsVipHouse(placeFromDataVo.getIsVipHouse());
        tbBizPlaceAttach.setIsTrain(placeFromDataVo.getIsTrain());
        tbBizPlaceAttach.setIsLockerRoom(placeFromDataVo.getIsLockerRoom());
        tbBizPlaceAttach.setPlaceGoods(placeFromDataVo.getPlaceGoods());
        tbBizPlaceAttach.setBookingConsultPhone(placeFromDataVo.getBookingConsultPhone()); // 订场咨询电话
        if(CollectionUtils.isNotEmpty(placeFromDataVo.getTrafficInfoList())){
            List<RespBizPlaceTrafficInfo> trafficInfoList = placeFromDataVo.getTrafficInfoList();
            String trafficInfo = "";
            for (RespBizPlaceTrafficInfo respBizPlaceTrafficInfo : trafficInfoList) {
                trafficInfo += respBizPlaceTrafficInfo.getType() + ":" + respBizPlaceTrafficInfo.getDescribe() + "|";
            }
            trafficInfo = trafficInfo.substring(0,trafficInfo.length() -1);
            tbBizPlaceAttach.setTrafficInfo(trafficInfo);
        }


        placeAttachMapper.updateByPlaceIdSelective(tbBizPlaceAttach);

        TbPlaceImageExample tbPlaceImageExample = new TbPlaceImageExample();
        TbPlaceImageExample.Criteria tbPlaceImageCriteria = tbPlaceImageExample.createCriteria();
        tbPlaceImageCriteria.andTopicIdEqualTo(placeFromDataVo.getId());
        tbPlaceImageMapper.deleteByExample(tbPlaceImageExample);


        if(tbPlaceImageList != null && tbPlaceImageList.size() > 0){
            for (TbPlaceImage tbPlaceImage : tbPlaceImageList){//插入场地图片资源表
                Byte bb = 1;
                tbPlaceImage.setContentType(bb);
                tbPlaceImage.setTopicId(tbBizPlace.getId());
                tbPlaceImage.setTitle(placeFromDataVo.getName() + "图片");
                tbPlaceImage.setSize(0);
                tbPlaceImageMapper.insert(tbPlaceImage);
            }
        }

        List<Integer> placeCategoryList = placeFromDataVo.getPlaceCategoryList();
        if(placeCategoryList != null && placeCategoryList.size() > 0){
            TbBizPlaceCategoryExample tbBizPlaceCategoryExample = new TbBizPlaceCategoryExample();
            TbBizPlaceCategoryExample.Criteria tbBizPlaceCategoryCriteria = tbBizPlaceCategoryExample.createCriteria();
            tbBizPlaceCategoryCriteria.andPlaceIdEqualTo(placeFromDataVo.getId());
            tbBizPlaceCategoryMapper.deleteByExample(tbBizPlaceCategoryExample);

            for (Integer categoryId : placeCategoryList) {
                TbBizPlaceCategory tbBizPlaceCategory = new TbBizPlaceCategory();
                tbBizPlaceCategory.setPlaceId(tbBizPlace.getId());
                tbBizPlaceCategory.setCategoryId(categoryId);
                tbBizPlaceCategoryMapper.insert(tbBizPlaceCategory);
            }
        }

        // 增加索引
        try{
            this.searchService.addPlaceIndex(tbBizPlace, null);
        }catch (Exception e){
            logger.error("增加索引失败!");
        }

        return APIResponse.returnSuccess();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse deletePlace(Integer placeId) {
        if(!placeBuyService.isDeletePlace(placeId,1)){
            return APIResponse.returnFail("场馆签约还在合同期内，删除失败。");
        }
        TbBizPlace tbBizPlace = new TbBizPlace();
        tbBizPlace.setIsDelete(Constants.IS_YES);
        tbBizPlace.setId(placeId);
        placeMapper.updateByPrimaryKeySelective(tbBizPlace);

        TbBizPlaceUnit tbBizPlaceUnit = new TbBizPlaceUnit();
        tbBizPlaceUnit.setIsDelete(Constants.IS_DELETE_TRUE);
        tbBizPlaceUnit.setPlaceId(placeId);
        placeUnitMapper.updateByPlaceId(tbBizPlaceUnit);

        TbBizPlaceUnitStatisticsExample tbBizPlaceUnitStatisticsExample = new TbBizPlaceUnitStatisticsExample();
        TbBizPlaceUnitStatisticsExample.Criteria tbBizPlaceUnitStatisticsCriteria = tbBizPlaceUnitStatisticsExample.createCriteria();
        tbBizPlaceUnitStatisticsCriteria.andPlaceIdEqualTo(placeId);
        tbBizPlaceUnitStatisticsMapper.deleteByExample(tbBizPlaceUnitStatisticsExample);

        return APIResponse.returnSuccess();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse deletePlaceUnit(Integer placeUnitId) {
        if(!placeBuyService.isDeletePlace(placeUnitId,2)){
            return APIResponse.returnFail("场地签约还在合同期内，删除失败。");
        }
        TbBizPlaceUnit tbBizPlaceUnit = new TbBizPlaceUnit();
        tbBizPlaceUnit.setIsDelete(Constants.IS_DELETE_TRUE);
        tbBizPlaceUnit.setId(placeUnitId);
        placeUnitMapper.updateByPrimaryKeySelective(tbBizPlaceUnit);

        TbBizPlaceUnitStatisticsExample tbBizPlaceUnitStatisticsExample = new TbBizPlaceUnitStatisticsExample();
        TbBizPlaceUnitStatisticsExample.Criteria tbBizPlaceUnitStatisticsCriteria = tbBizPlaceUnitStatisticsExample.createCriteria();
        tbBizPlaceUnitStatisticsCriteria.andPlaceUnitIdEqualTo(placeUnitId);
        tbBizPlaceUnitStatisticsMapper.deleteByExample(tbBizPlaceUnitStatisticsExample);


        return APIResponse.returnSuccess();
    }

    @Override
    public PageObj<List<TbBizPlaceUnit>> getAllPlaceUnitList(ReqPlaceUnitSearch reqPlaceUnitSearch) {
        Integer pageNum = 0;
        Integer pageSize = Constants.DEFAULT_PAGE_SIZE;
        if (reqPlaceUnitSearch != null){
            pageNum = reqPlaceUnitSearch.getPageNum();
            pageSize = reqPlaceUnitSearch.getPageSize();
        }
        int total = placeUnitListMapper.countPlaceUnitList(reqPlaceUnitSearch);
        reqPlaceUnitSearch.setPage(new Page(pageNum * pageSize, pageSize, total));
        List<TbBizPlaceUnit> records = placeUnitListMapper.selectPlaceUnitList(reqPlaceUnitSearch);
        PageObj<List<TbBizPlaceUnit>> result = PageObj.create(total, pageNum, pageSize, records);
        return result;
    }*/

    @Override
    public List<PlaceUnitDubboProvider> getPlaceUnitListByPlaceId(Integer placeId) {
        TbBizPlaceUnitExample example= new TbBizPlaceUnitExample();
        TbBizPlaceUnitExample.Criteria criterial = example.createCriteria();
        if (placeId != null){
            criterial.andPlaceIdEqualTo(placeId);
        }
        criterial.andIsDeleteEqualTo((byte)0);  // 未删除的场地

        // 获取场馆下可用的场地列表
        List<TbBizPlaceUnit> tbBizPlaceUnitList = placeUnitMapper.selectByExample(example);
        TbBizPlace tbBizPlace = placeMapper.selectByPrimaryKey(placeId);
        List<PlaceUnitDubboProvider> placeUnitList =  new ArrayList<>();
        if(null != tbBizPlace){
            for(TbBizPlaceUnit tbBizPlaceUnit:tbBizPlaceUnitList){
                PlaceUnitDubboProvider placeUnitDubboProvider = new PlaceUnitDubboProvider();
                placeUnitDubboProvider.setCityId(tbBizPlace.getCityId());
                placeUnitDubboProvider.setLng(tbBizPlace.getLng());
                placeUnitDubboProvider.setPlaceId(tbBizPlaceUnit.getPlaceId());
                placeUnitDubboProvider.setDistrictId(tbBizPlaceUnit.getCategoryId());
                placeUnitDubboProvider.setAddress(tbBizPlace.getAddress());
                placeUnitDubboProvider.setName(tbBizPlaceUnit.getName());
                placeUnitDubboProvider.setPlaceUnitId(tbBizPlaceUnit.getId());
                placeUnitDubboProvider.setLat(tbBizPlace.getLat());
                placeUnitList.add(placeUnitDubboProvider);
            }
        }
        return placeUnitList;
    }

    @Override
    public List<PlaceDubboProvider> getPlaceListByCityIdOrDistrictId(Integer cityId, Integer districtId) {
        TbBizPlaceExample example = new TbBizPlaceExample();
        TbBizPlaceExample.Criteria criterial = example.createCriteria();
        if(cityId!=null){
            criterial.andCityIdEqualTo(cityId);
        }
        if(districtId!=null){
            criterial.andDistrictIdEqualTo(districtId);
        }
        criterial.andIsSignEqualTo(PlaceConstants.IS_SIGN_YES);//取签约场馆
        List<PlaceDubboProvider> placeDubboProviderList = new ArrayList<>();
        List<TbBizPlace> tbBizPlaceList = placeMapper.selectByExample(example);
        if(null != tbBizPlaceList){
            for(TbBizPlace tbBizPlace:tbBizPlaceList){
                PlaceDubboProvider placeDubboProvider = new PlaceDubboProvider();
                placeDubboProvider.setCityId(tbBizPlace.getCityId());
                placeDubboProvider.setName(tbBizPlace.getName());
                placeDubboProvider.setLat(tbBizPlace.getLat());
                placeDubboProvider.setLng(tbBizPlace.getLng());
                placeDubboProvider.setAddress(tbBizPlace.getAddress());
                placeDubboProvider.setDistrictId(tbBizPlace.getDistrictId());
                placeDubboProvider.setPlaceId(tbBizPlace.getId());
                placeDubboProviderList.add(placeDubboProvider);
            }
        }
        return placeDubboProviderList;
    }

    @Override
    public List<PlaceDubboProvider> getPlaceListByCityIdOrDistrictId(Integer cityId, Integer districtId, Integer categoryId) {
        Map<String,Integer> map = new HashMap<>();
        map.put("cityId", cityId);
        map.put("districtId", districtId);
        map.put("categoryId", categoryId);
        List<TbBizPlace> tbBizPlaceList = this.placeMapperBase.getPlaceBycityCategory(map);
        List<PlaceDubboProvider> placeDubboProviderList = new ArrayList<>();

        if(null != tbBizPlaceList){
            for(TbBizPlace tbBizPlace:tbBizPlaceList){
                PlaceDubboProvider placeDubboProvider = new PlaceDubboProvider();
                placeDubboProvider.setCityId(tbBizPlace.getCityId());
                placeDubboProvider.setName(tbBizPlace.getName());
                placeDubboProvider.setLat(tbBizPlace.getLat());
                placeDubboProvider.setLng(tbBizPlace.getLng());
                placeDubboProvider.setAddress(tbBizPlace.getAddress());
                placeDubboProvider.setDistrictId(tbBizPlace.getDistrictId());
                placeDubboProvider.setPlaceId(tbBizPlace.getId());
                placeDubboProviderList.add(placeDubboProvider);
            }
        }
        return placeDubboProviderList;
    }

    @Override
    public List<RespDistrictVo> getDistrictByCityId(int cityId) {
        TbDistrictExample tbDistrictExample = new TbDistrictExample();
        TbDistrictExample.Criteria tbDistrictCriteria = tbDistrictExample.createCriteria();
        tbDistrictCriteria.andCityIdEqualTo(cityId);
        tbDistrictExample.setOrderByClause("priority");
        List<TbDistrict> list = tbDistrictMapper.selectByExample(tbDistrictExample);
        List<RespDistrictVo> respDistrictsList = new ArrayList<RespDistrictVo>();
        if(!CollectionUtils.isEmpty(list)){
            for(TbDistrict tbDistrict : list){
                RespDistrictVo respDistrict = new RespDistrictVo();
                respDistrict.setName(tbDistrict.getName());
                respDistrict.setId(tbDistrict.getId());
                respDistrict.setCityId(cityId);
                respDistrict.setDesc(tbDistrict.getName());
                respDistrict.setLat(tbDistrict.getLat());
                respDistrict.setLng(tbDistrict.getLng());
                respDistrict.setPinyin(tbDistrict.getPinyin());
                if (!org.springframework.util.StringUtils.isEmpty(tbDistrict.getPinyin())){
                    respDistrict.setInitial(tbDistrict.getPinyin().charAt(0)+"");
                }
                respDistrictsList.add(respDistrict);
            }
        }
        return respDistrictsList;
    }

    @Override
    public Map<Integer, PlaceUnitDubboProvider> getPlaceUnitListByPlaceUnitId(List placeUnitIds) {
        Map<Integer, PlaceUnitDubboProvider> placeUnitMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(placeUnitIds)){
            if (PlaceConstants.PLACE_DUBBO_REQUEST_LIMIT >= placeUnitIds.size()){
                TbBizPlaceUnitExample example = new TbBizPlaceUnitExample();
                TbBizPlaceUnitExample.Criteria criteria = example.createCriteria();
                criteria.andIdIn(placeUnitIds);
                List<TbBizPlaceUnit> tbBizPlaceUnits = placeUnitMapper.selectByExample(example);
                for (TbBizPlaceUnit tbBizPlaceUnit : tbBizPlaceUnits){
                    PlaceUnitDubboProvider placeUnitDubboProvider = new PlaceUnitDubboProvider();
                    TbBizPlace tbBizPlace = placeMapper.selectByPrimaryKey(tbBizPlaceUnit.getPlaceId());
                    placeUnitDubboProvider.setCityId(tbBizPlace.getCityId());
                    placeUnitDubboProvider.setLng(tbBizPlace.getLng());
                    placeUnitDubboProvider.setPlaceId(tbBizPlaceUnit.getPlaceId());
                    placeUnitDubboProvider.setDistrictId(tbBizPlaceUnit.getCategoryId());
                    placeUnitDubboProvider.setAddress(tbBizPlace.getAddress());
                    placeUnitDubboProvider.setName(tbBizPlaceUnit.getName());
                    placeUnitDubboProvider.setPlaceUnitId(tbBizPlaceUnit.getId());
                    placeUnitDubboProvider.setLat(tbBizPlace.getLat());
                    placeUnitMap.put(tbBizPlaceUnit.getId(),placeUnitDubboProvider);
                }
            }
        }
        return placeUnitMap;
    }

    @Override
    public Map<Integer, PlaceDubboProvider> getPlaceListByPlaceId(List placeIds) {
        Map<Integer, PlaceDubboProvider> placeMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(placeIds)){
            if (PlaceConstants.PLACE_DUBBO_REQUEST_LIMIT >= placeIds.size()){
                TbBizPlaceExample example = new TbBizPlaceExample();
                TbBizPlaceExample.Criteria criteria = example.createCriteria();
                criteria.andIdIn(placeIds);
                List<TbBizPlace> tbBizPlaces = placeMapper.selectByExample(example);
                for (TbBizPlace tbBizPlace : tbBizPlaces){
                    PlaceDubboProvider placeDubboProvider = new PlaceDubboProvider();
                    placeDubboProvider.setCityId(tbBizPlace.getCityId());
                    placeDubboProvider.setName(tbBizPlace.getName());
                    placeDubboProvider.setLat(tbBizPlace.getLat());
                    placeDubboProvider.setLng(tbBizPlace.getLng());
                    placeDubboProvider.setAddress(tbBizPlace.getAddress());
                    placeDubboProvider.setDistrictId(tbBizPlace.getDistrictId());
                    placeDubboProvider.setPlaceId(tbBizPlace.getId());
                    TbCity tbCity = this.getCityDataById(tbBizPlace.getCityId());
                    if (!org.springframework.util.StringUtils.isEmpty(tbCity)) {
                        placeDubboProvider.setCityName(tbCity.getName());
                    }
                    TbDistrict tbDistrict = tbDistrictMapper.selectByPrimaryKey(tbBizPlace.getDistrictId());
                    if (!org.springframework.util.StringUtils.isEmpty(tbDistrict)){
                        placeDubboProvider.setDestrictName(tbDistrict.getName());
                    }
                    placeMap.put(tbBizPlace.getId(),placeDubboProvider);
                }
            }
        }
        return placeMap;
    }

    @Override
    public Map<Integer, PlaceDubboProvider> getPlaceBySinglePlaceId(Integer placeId) {
        Map<Integer, PlaceDubboProvider> placeMap = new HashMap<>();
        if(!StringUtils.isEmpty(placeId)){
            TbBizPlace tbBizPlace = placeMapper.selectByPrimaryKey(placeId);
            if (!StringUtils.isEmpty(tbBizPlace)){
                PlaceDubboProvider placeDubboProvider = new PlaceDubboProvider();
                placeDubboProvider.setCityId(tbBizPlace.getCityId());
                placeDubboProvider.setName(tbBizPlace.getName());
                placeDubboProvider.setLat(tbBizPlace.getLat());
                placeDubboProvider.setLng(tbBizPlace.getLng());
                placeDubboProvider.setAddress(tbBizPlace.getAddress());
                placeDubboProvider.setDistrictId(tbBizPlace.getDistrictId());
                placeDubboProvider.setPlaceId(tbBizPlace.getId());
                TbCity tbCity = this.getCityDataById(tbBizPlace.getCityId());
                if (!org.springframework.util.StringUtils.isEmpty(tbCity)) {
                    placeDubboProvider.setCityName(tbCity.getName());
                }
                TbDistrict tbDistrict = tbDistrictMapper.selectByPrimaryKey(tbBizPlace.getDistrictId());
                if (!org.springframework.util.StringUtils.isEmpty(tbDistrict)){
                    placeDubboProvider.setDestrictName(tbDistrict.getName());
                }
                placeMap.put(tbBizPlace.getId(),placeDubboProvider);
            }
        }
        return placeMap;
    }

    private Integer updatePlaceGroupId(Integer placeId,String placeName,Integer uid){
        Map<String,Object> paramMap = new HashMap();
        paramMap.put("groupname",placeName);               //群组名称
        paramMap.put("ispublic",0);                        //公开群
        paramMap.put("maxusers",1000);                     //群组成员最大数
        paramMap.put("approval",1);                        //不需要批准
        paramMap.put("type",5);                            //场馆类型
        paramMap.put("groupdescriptions",placeName);       //群组描述
        paramMap.put("owner",uid);                         //请求用户的Uid  678测试可用
        paramMap.put("ver","1.1.0000");
        paramMap.put("protocol_ver","1.0");
        paramMap.put("currentUid",uid);

        String json = JSON.toJSONString(paramMap);

        String result = null;
        try {
            result = HttpClientUtils.postJson(cAddress + "/IM/createGroup.api", json);
            JSONObject jsonObject = JSONObject.parseObject(result);
            String data = jsonObject.getString("data");
            JSONObject jsondata = JSONObject.parseObject(data);
            JSONArray jsonArray  = jsondata.getJSONArray("groupinfo");
            Object[] arrayObjects = jsonArray.toArray();
            PlaceGroupInfo placeGroupInfo = JSON.parseObject(arrayObjects[0].toString(), PlaceGroupInfo.class);
            logger.info("获取场馆聊天群号：" + result);
            TbBizPlace tbBizPlace = new TbBizPlace();
            tbBizPlace.setId(placeId);
            tbBizPlace.setGroupId(placeGroupInfo.getGroupid());
            return placeMapper.updateByPrimaryKeySelective(tbBizPlace);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * getUserPlaceHours
     * @param uid
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public Double getUserPlaceHours(Integer uid, Date startDate, Date endDate) {
        Map<String,Object> queryMap = new HashMap<>();
        if (uid != null){
            queryMap.put("uid",uid);
        }

        if (startDate != null){
            queryMap.put("startDate",DateUtils.transferDateToString(startDate));
        }
        if (endDate != null){
            queryMap.put("endDate",DateUtils.transferDateToString(endDate));
        }
        return  tbBizPlaceOrderManagerMapper.getUserPlaceHours(queryMap);
    }

    @Override
    public RespDistrict getRespDistrict(double lat, double lng) {
        RespDistrict respDistrict = new RespDistrict();
        LBSApi lbsApi = new BaiduLBS();
        AddressComponent addressComponent = lbsApi.getAddressComponentByLatAndLng(lat, lng);
        if (StringUtils.isEmpty(addressComponent)) {
            return null;
        }
        //截取“市”之前的信息，这样的话就可以处理遇到的问题
        String cityName = addressComponent.getCity();
        String districtName = addressComponent.getDistrict();
        if (cityName.lastIndexOf("市") != -1 && districtName.lastIndexOf("区") != -1) {
            cityName = cityName.substring(0, cityName.lastIndexOf("市"));
            TbCityExample example = new TbCityExample();
            TbCityExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike(cityName);  //模糊匹配城市
            int count = tbCityMapper.countByExample(example);
            if (count == 1) {
                Integer cityId = tbCityMapper.selectByExample(example).get(0).getId();
                if (cityId != null) {
                    TbDistrictExample  exampleTemp = new TbDistrictExample();
                    TbDistrictExample.Criteria criteriaTemp = exampleTemp.createCriteria();
                    criteriaTemp.andCityIdEqualTo(cityId);
                    criteriaTemp.andNameLike(districtName.substring(0, districtName.lastIndexOf("区")) + "%");
                    List<TbDistrict> tbDistrictList = tbDistrictMapper.selectByExample(exampleTemp);
                    if (CollectionUtils.isNotEmpty(tbDistrictList)) {
                        try {
                            BeanUtils.copyProperties(respDistrict, tbDistrictList.get(0));
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return respDistrict;
    }

    /**
     * 根据场馆id获取地理位置（批量）
     * @param placeIds
     * @return
     */
    @Override
    public List<RespPlaceLocation> getPlaceLocation(List placeIds) {
        List<RespPlaceLocation> respPlaceLocationList = new ArrayList<RespPlaceLocation>();
        StringBuffer strBuffPlaceIds = new StringBuffer();
        if (null == placeIds || placeIds.size() == 0){
            return respPlaceLocationList;
        }
        strBuffPlaceIds.append("(");
        for (Object obj : placeIds){
            Integer placeId = (Integer) obj;
            strBuffPlaceIds.append(placeId);
            strBuffPlaceIds.append(",");
        }
        int index = strBuffPlaceIds.toString().lastIndexOf(',');
        if(index != -1){
            strBuffPlaceIds.deleteCharAt(index);
        }
        strBuffPlaceIds.append(")");
        respPlaceLocationList = vPlaceMapper.getPlaceLocation(strBuffPlaceIds.toString());
        return respPlaceLocationList;
    }

    /**
     * 用户是否订过场
     * @param uid
     * @return
     */
    @Override
    public boolean isAnyOrder(Integer uid) {
        Integer orderCount = vPlaceMapper.countPlaceOrder(uid, Constants.BYTE_ONE);
        return (null != orderCount && orderCount >0) ? true : false;
    }


    /**
     * 用户会员卡数量
     * @param uid
     * @return
     */
    @Override
    public Integer getMemberCardNum(Integer uid) {
        Integer memberNum = vPlaceMemberMapper.countPlaceMemberCardByUid(uid);
        return memberNum;
    }

    @Override
    public PlaceBaseResponse getPlaceById(Integer placeId) {
        TbBizPlace tbBizPlace = placeMapper.selectByPrimaryKey(placeId);
        PlaceBaseResponse placeBaseResponse = new PlaceBaseResponse();
        if (null != tbBizPlace){
            org.springframework.beans.BeanUtils.copyProperties(tbBizPlace,placeBaseResponse);
        }
        return placeBaseResponse;
    }

    @Override
    public APIResponse<List<Integer>> findOrdersByUid(Integer uid) {
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        tbBizPlaceOrderExample.createCriteria().andUidEqualTo(uid).andIsEffectiveEqualTo((byte)1);
        tbBizPlaceOrderExample.setDistinct(true);
        tbBizPlaceOrderExample.setOrderByClause("create_time desc");
        List<TbBizPlaceOrder> tbBizPlaceOrders = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
        List<Integer> placeids = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(tbBizPlaceOrders)){
            for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders){
                placeids.add(tbBizPlaceOrder.getPlaceId());
            }
        }
        if (CollectionUtils.isNotEmpty(placeids)){
            List<Integer> result = new ArrayList<Integer>(new HashSet<>(placeids));
            return APIResponse.returnSuccess(result );
        }
        return APIResponse.returnFail(ApiResponseEnum.DO_NOT_HAVE_ANY_MORE_RECORD);
    }

    @Override
    public APIResponse<MyPlaceResponse> findEarlyPlace(Integer uid) {
        List<RespRecommendPlaceOrder> list  = placeMapperBase.orderPlaceByUid24(uid);
        if (CollectionUtils.isEmpty(list)){
            return APIResponse.returnFail(ApiResponseEnum.DO_NOT_HAVE_ANY_MORE_RECORD);
        }
        MyPlaceResponse myPlaceResponse = new MyPlaceResponse();

        Date now = new Date();
        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        for (RespRecommendPlaceOrder respRecommendPlaceOrder : list){
            String strDateTime = DateUtils.convertFormatDateToString(DateUtils
                    .DATE_FORMAT_yyyy_MM_dd,new Date(respRecommendPlaceOrder.getStartDate().getTime()))
                    + " " + df.format(new Date(respRecommendPlaceOrder.getStartTime().getTime()));
            Long date = DateUtils.strToDate(strDateTime,
                    DateUtils.DAY_DATE_FORMAT_HH_MM_SS).getTime();
            if (date - now.getTime() > 0){
                myPlaceResponse.setPlaceId(respRecommendPlaceOrder.getPlaceId());
                myPlaceResponse.setPlaceName(respRecommendPlaceOrder.getPlaceName());
                myPlaceResponse.setCategoryId(respRecommendPlaceOrder.getCategoryId());
                myPlaceResponse.setStartDate(respRecommendPlaceOrder.getStartDate().getTime());
                if (null != myPlaceResponse.getCategoryId()){
                    Cagetory tbBizPlaceCategory = cagetoryMapper
                            .selectByPrimaryKey(respRecommendPlaceOrder.getCategoryId());
                    if (null != tbBizPlaceCategory){
                        myPlaceResponse.setCategoryName(tbBizPlaceCategory.getName());
                    }
                }
                myPlaceResponse.setStartTime(respRecommendPlaceOrder.getStartTime().getTime());
                myPlaceResponse.setEndTime(respRecommendPlaceOrder.getEndTime().getTime());
                break;
            }
        }

        return APIResponse.returnSuccess(myPlaceResponse);
    }

    @Override
    public APIResponse<PlaceSearchResponse> findPlaceByLat(Double lat, Double lng) {
        PlaceSearchService placeSearchService = new PlaceSearchServiceImpl(solrUrl);
        ReqPlaceSearchInfo reqPlaceSearchInfo = new ReqPlaceSearchInfo();
        reqPlaceSearchInfo.setLat(lat);
        reqPlaceSearchInfo.setLng(lng);
        reqPlaceSearchInfo.setSortId(3);
        reqPlaceSearchInfo.setDistance(20000D);
        reqPlaceSearchInfo.setPageNo(0);
        reqPlaceSearchInfo.setPageSize(1);
        APIResponse<SearchResponses<List<com.quanyan.search.entity.response.PlaceSearchResponse>>> response
                = placeSearchService.searchPlaceInfoList(reqPlaceSearchInfo);
        if (response.isRet()){
            SearchResponses<List<com.quanyan.search.entity.response.PlaceSearchResponse>> searchResponses = response.getData();
            List<com.quanyan.search.entity.response.PlaceSearchResponse> places =  searchResponses.getData();
            if (CollectionUtils.isNotEmpty(places)){
                PlaceSearchResponse place = new PlaceSearchResponse();
                try {
                    BeanUtils.copyProperties(place, places.get(0));
                    return APIResponse.returnSuccess(place);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return APIResponse.returnFail("没有场馆信息");
    }
}
